package com.agua.enturno.vista.paginas.adm.agendas.jornadas;

import com.agua.enturno.util.DayOfWeekTime;
import com.agua.enturno.vista.dto.JornadaLaboralHabitualDTO;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxFallbackLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.joda.time.DateTimeConstants;
import org.joda.time.Duration;

import java.util.List;


public class JornadasHabitualesPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	private IModel<List<JornadaLaboralHabitualDTO>> model;
	private IModel<Long> modelAgendaId;
	
	public JornadasHabitualesPanel(String id, IModel<List<JornadaLaboralHabitualDTO>> model, IModel<Long> modelAgendaId) {
		super(id, model);
		this.setOutputMarkupId(true);
		this.model = model;
		this.modelAgendaId = modelAgendaId;
		
		add(new ListView<JornadaLaboralHabitualDTO>("jornada-habitual", model) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<JornadaLaboralHabitualDTO> item) {
				item.add(new JornadaHabitualPanel("jornada-habitual-interna", new PropertyModel<JornadaLaboralHabitualDTO>(item, "modelObject")));
			}
			
		});
		
		add(new AjaxFallbackLink<String>("agregar-jornada-habitual") {

			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				agregarJornadaHabitual();
				target.add(JornadasHabitualesPanel.this.getParent());
			}
		});
		
	}
	
	private void agregarJornadaHabitual() {
		this.model.getObject().add(new JornadaLaboralHabitualDTO(modelAgendaId.getObject(), null, new DayOfWeekTime(DateTimeConstants.MONDAY, 9, 0), new DayOfWeekTime(DateTimeConstants.MONDAY, 18, 0), Duration.standardHours(1), 1));
	}

}
